<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 公共控制模块
// +----------------------------------------------------------------------
namespace app\common\controller;

use think\Controller;
use think\Response;
use think\exception\HttpResponseException;
use think\facade\Lang;
use app\common\common\Helper;
use app\common\constant\Constants;

class Base extends Controller
{
    protected $_model;

    protected function initialize() {
        parent::initialize();
        defined('MODULE_NAME') OR define('MODULE_NAME', strtolower(request()->module()));
        defined('CONTROLLER_NAME') OR define('CONTROLLER_NAME', strtolower(request()->controller()));
        defined('ACTION_NAME') OR define('ACTION_NAME', request()->action());
        //加载多语言相应控制器对应字段
        if(config('lang_switch_on') && isset($_COOKIE['think_var']) && $_COOKIE['think_var']){
            $langField = $_COOKIE['think_var'];
        }else{
            $langField = config('default_lang');
        }
        Lang::load(APP_PATH . MODULE_NAME.'/lang/'.$langField.'/'.CONTROLLER_NAME.'.php');
    }

    public function isPost() {
        $isPost = $this->request->isPost();
        return $isPost;
    }

    public function isJson() {
        $isJson = $this->request->isAjax();
        return $isJson;
    }

    /**
     * 输出返回JSON数据
     */
    public static function json($data)
    {
        return Helper::json($data);
    }

    /**
     * 输出返回JSON数据
     */
    public static function jsonResult($data = [], $msg = Constants::JSON_MESSAGE_SUCCESS, $code = Constants::JSON_STATUS_SUCCESS, $extra = [])
    {
        return Helper::jsonResult($data, $msg, $code, $extra);
    }

    /**
     * 输出返回JSON数据
     */
    public static function jsonSuccess($data = [], $extra = [], $msg = Constants::JSON_MESSAGE_SUCCESS)
    {
        return Helper::jsonSuccess($data, $extra, $msg);
    }

    /**
     * 输出返回JSON错误信息
     */
    public static function jsonError($msg = Constants::JSON_MESSAGE_EMPTY, $code = Constants::JSON_STATUS_ERROR, $data = [])
    {
        return Helper::jsonError($msg, $code, $data);
    }

    //空操作
    public function _empty()
    {
        $this->error('该页面不存在！');
    }
}
